define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格配置
            var tableConfig = {
                extend: {
                    index_url: 'slider/slider/index' + location.search,
                    add_url: 'slider/slider/add',
                    edit_url: 'slider/slider/edit',
                    del_url: 'slider/slider/del',
                    multi_url: 'slider/slider/multi',
                    import_url: 'slider/slider/import',
                    table: '#table-city-home',
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'url', title: __('Url'), formatter: Table.api.formatter.url},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'status', title: __('Status'), searchList: {"normal":__("Normal"),"hidden":__("Hidden")}, formatter: Table.api.formatter.status},
                        {field: 'create_time', title: __('Create time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: '#table-city-home', events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            };

            // 城市首页表格
            var tableCityHome = Table.bootstrap.init($.extend(true, {}, tableConfig, {
                extend: $.extend(true, {}, tableConfig.extend, {
                    index_url: 'slider/slider/index?type=city-home',
                    table: '#table-city-home'
                }),
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'url', title: __('Url'), formatter: Table.api.formatter.url},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'status', title: __('Status'), searchList: {"normal":__("Normal"),"hidden":__("Hidden")}, formatter: Table.api.formatter.status},
                        {field: 'create_time', title: __('Create time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: '#table-city-home', events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            }));

            // 教学点首页表格
            var tableTeachingPoint = null;
            
            // 班级首页表格
            var tableClassHome = null;
            
            // 社区学科表格
            var tableCommunitySubject = null;

            // 学校首页表格
            var tableSchoolHome = null;
            
            // 其他表格
            var tableOthers = null;

            // 选项卡切换事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var target = $(e.target).attr("href");
                var tableId = target.replace('#', '#table-');

                
                switch(target) {
                    case '#city-home':
                        if (tableCityHome) {
                            tableCityHome.bootstrapTable('refresh');
                        }
                        break;
                    case '#teaching-point':
                        if (!tableTeachingPoint) {
                            tableTeachingPoint = Table.bootstrap.init($.extend(true, {}, tableConfig, {
                                extend: $.extend(true, {}, tableConfig.extend, {
                                    index_url: 'slider/slider/index?type=teaching-point',
                                    table: '#table-teaching-point'
                                }),
                                columns: tableConfig.columns
                            }));
                        } else {
                            tableTeachingPoint.bootstrapTable('refresh');
                        }
                        break;
                    case '#school-home':
                        if (!tableSchoolHome) {
                            tableSchoolHome = Table.bootstrap.init($.extend(true, {}, tableConfig, {
                                extend: $.extend(true, {}, tableConfig.extend, {
                                    index_url: 'slider/slider/index?type=school-home',
                                    table: '#table-school-home'
                                }),
                                columns: tableConfig.columns
                            }));
                        } else {
                            tableSchoolHome.bootstrapTable('refresh');
                        }
                        break;
                    case '#class-home':
                        if (!tableClassHome) {
                            tableClassHome = Table.bootstrap.init($.extend(true, {}, tableConfig, {
                                extend: $.extend(true, {}, tableConfig.extend, {
                                    index_url: 'slider/slider/index?type=class-home',
                                    table: '#table-class-home'
                                }),
                                columns: tableConfig.columns
                            }));
                        } else {
                            tableClassHome.bootstrapTable('refresh');
                        }
                        break;
                    case '#community-subject':
                        if (!tableCommunitySubject) {
                            tableCommunitySubject = Table.bootstrap.init($.extend(true, {}, tableConfig, {
                                extend: $.extend(true, {}, tableConfig.extend, {
                                    index_url: 'slider/slider/index?type=community-subject',
                                    table: '#table-community-subject'
                                }),
                                columns: tableConfig.columns
                            }));
                        } else {
                            tableCommunitySubject.bootstrapTable('refresh');
                        }
                        break;
                    case '#others':
                        if (!tableOthers) {
                            tableOthers = Table.bootstrap.init($.extend(true, {}, tableConfig, {
                                extend: $.extend(true, {}, tableConfig.extend, {
                                    index_url: 'slider/slider/index?type=others',
                                    table: '#table-others'
                                }),
                                columns: tableConfig.columns
                            }));
                        } else {
                            tableOthers.bootstrapTable('refresh');
                        }
                        break;
                }
            });

            // 为表格操作按钮添加事件
            Table.api.bindevent($('#table-city-home'));
            
            // 监听选项卡切换后绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var target = $(e.target).attr("href");
                var tableSelector = target.replace('#', '#table-');
                Table.api.bindevent($(tableSelector));
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});